package hieudd.appgame.andengine;

import javax.microedition.khronos.opengles.GL10;

import org.anddev.andengine.engine.Engine;
import org.anddev.andengine.engine.camera.Camera;
import org.anddev.andengine.engine.camera.hud.controls.AnalogOnScreenControl;
import org.anddev.andengine.engine.camera.hud.controls.BaseOnScreenControl;
import org.anddev.andengine.engine.camera.hud.controls.AnalogOnScreenControl.IAnalogOnScreenControlListener;
import org.anddev.andengine.engine.handler.physics.PhysicsHandler;
import org.anddev.andengine.engine.options.EngineOptions;
import org.anddev.andengine.engine.options.EngineOptions.ScreenOrientation;
import org.anddev.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.anddev.andengine.entity.modifier.IEntityModifier;
import org.anddev.andengine.entity.modifier.MoveModifier;
import org.anddev.andengine.entity.modifier.ScaleModifier;
import org.anddev.andengine.entity.modifier.SequenceEntityModifier;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.scene.background.AutoParallaxBackground;
import org.anddev.andengine.entity.scene.background.ColorBackground;
import org.anddev.andengine.entity.scene.background.ParallaxBackground.ParallaxEntity;
import org.anddev.andengine.entity.sprite.AnimatedSprite;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.entity.util.FPSLogger;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.anddev.andengine.opengl.texture.region.TextureRegion;
import org.anddev.andengine.opengl.texture.region.TiledTextureRegion;
import org.anddev.andengine.ui.activity.BaseGameActivity;

import android.text.TextUtils;
import android.util.Log;
import android.view.Display;
import android.widget.Toast;

public class newSceneGame extends BaseExample {

	private static final int CAMERA_WIDTH = 480;
	private static final int CAMERA_HEIGHT = 760;

	private Camera mCamera;

	private BitmapTextureAtlas mAirBitmapTextureAtlas;
	private TextureRegion mAirTextureRegion;

	private BitmapTextureAtlas mBackgroundAutoRunAtlas;
	private TextureRegion mBackgroundTR;

	private BitmapTextureAtlas mOnScreenControlTextureAtlas;
	private TextureRegion mOnScreenControlBaseTextureRegion;
	private TextureRegion mOnScreenControlKnobTextureRegion;

	private BitmapTextureAtlas mRocketBitmapTextureAtlas;
	private TextureRegion mRocketTextureRigion;

	private BitmapTextureAtlas mNewRocketBitmapTextureAtlas;
	private TextureRegion mNewRocketTextureRegion;

	// target
	private TextureRegion mTargetTextureRegion;
	private TextureRegion mSocketTR;

	private static int rocketX;
	private static int rocketY;

	public Engine onLoadEngine() {
		Toast.makeText(this, "Also try tapping this AnalogOnScreenControl!",
				Toast.LENGTH_LONG).show();
		this.mCamera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
		return new Engine(new EngineOptions(true, ScreenOrientation.PORTRAIT,
				new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT),
				this.mCamera));
	}

	public void onLoadResources() {
		BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");

		// air war
		this.mAirBitmapTextureAtlas = new BitmapTextureAtlas(1024, 1024,
				TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		this.mAirTextureRegion = BitmapTextureAtlasTextureRegionFactory
				.createFromAsset(this.mAirBitmapTextureAtlas, this,
						"airwar.png", 0, 0);

		// background
		this.mBackgroundAutoRunAtlas = new BitmapTextureAtlas(1024, 1024,
				TextureOptions.DEFAULT);
		this.mBackgroundTR = BitmapTextureAtlasTextureRegionFactory
				.createFromAsset(this.mBackgroundAutoRunAtlas, this,
						"roadRun.png", 0, 188);

		// control
		this.mOnScreenControlTextureAtlas = new BitmapTextureAtlas(256, 128,
				TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		this.mOnScreenControlBaseTextureRegion = BitmapTextureAtlasTextureRegionFactory
				.createFromAsset(this.mOnScreenControlTextureAtlas, this,
						"onscreen_control_base.png", 0, 0);
		this.mOnScreenControlKnobTextureRegion = BitmapTextureAtlasTextureRegionFactory
				.createFromAsset(this.mOnScreenControlTextureAtlas, this,
						"onscreen_control_knob.png", 128, 0);

		// rocket
		this.mRocketBitmapTextureAtlas = new BitmapTextureAtlas(1024, 1024,
				TextureOptions.BILINEAR);
		this.mRocketTextureRigion = BitmapTextureAtlasTextureRegionFactory
				.createFromAsset(this.mRocketBitmapTextureAtlas, this,
						"Rocket.png", 0, 0);

		this.mEngine.getTextureManager().loadTextures(
				this.mAirBitmapTextureAtlas, this.mOnScreenControlTextureAtlas,
				this.mBackgroundAutoRunAtlas, this.mRocketBitmapTextureAtlas);
	}

	public Scene onLoadScene() {
		this.mEngine.registerUpdateHandler(new FPSLogger());

		// new Scene, setBackground

		final Scene scene = new Scene();
		scene.setBackground(new ColorBackground(0.09804f, 0.6274f, 0.8784f));

		final Sprite airWar = new Sprite(0, 0, this.mAirTextureRegion);

		// create background roadrun
		final AutoParallaxBackground autoParallaxBgr = new AutoParallaxBackground(
				0.9f, 0.7f, 0.9f, 5);
		autoParallaxBgr.attachParallaxEntity(new ParallaxEntity(-5.0f,
				new Sprite(0, 570, this.mBackgroundTR)));
		scene.setBackground(autoParallaxBgr);

		// handler airwar
		final PhysicsHandler physicsHandler = new PhysicsHandler(airWar);
		airWar.registerUpdateHandler(physicsHandler);

		scene.attachChild(airWar);

		final AnalogOnScreenControl analogOnScreenControl = new AnalogOnScreenControl(
				0, CAMERA_HEIGHT
						- this.mOnScreenControlBaseTextureRegion.getHeight(),
				this.mCamera, this.mOnScreenControlBaseTextureRegion,
				this.mOnScreenControlKnobTextureRegion, 0.1f, 200,
				new IAnalogOnScreenControlListener() {
					public void onControlChange(
							final BaseOnScreenControl pBaseOnScreenControl,
							final float pValueX, final float pValueY) {
						physicsHandler
								.setVelocity(pValueX * 100, pValueY * 100);

					}

					public void onControlClick(
							final AnalogOnScreenControl pAnalogOnScreenControl) {

						synchronized (pAnalogOnScreenControl) {

							rocketX = (int) airWar.getX();
							rocketY = (int) airWar.getY();
							Sprite socketSprite = new Sprite(rocketX, rocketY
									+ mRocketTextureRigion.getHeight() / 2,
									mRocketTextureRigion);
							socketSprite
									.registerEntityModifier(new MoveModifier(2,
											rocketX, 500, rocketY
													+ mRocketTextureRigion
															.getHeight() / 2,
											rocketY
													+ mRocketTextureRigion
															.getHeight() / 2));
							scene.attachChild(socketSprite);
						}

					}
				});
		analogOnScreenControl.getControlBase().setBlendFunction(
				GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
		analogOnScreenControl.getControlBase().setAlpha(0.5f);
		analogOnScreenControl.getControlBase().setScaleCenter(0, 128);
		analogOnScreenControl.getControlBase().setScale(1.25f);
		analogOnScreenControl.getControlKnob().setScale(1.25f);
		analogOnScreenControl.refreshControlKnobPosition();

		scene.setChildScene(analogOnScreenControl);

		return scene;
	}

	public void onLoadComplete() {

	}

}
